const RecordService = require('../../service/record')
const Response = require('../../utils/response')

async function newRecord(ctx) {
    ctx.request.body.status = 0
    const {
        Job_id,
        user_id
    } = ctx.request.body
    const ishas = await RecordService.getRecordHas({
        Job_id: Job_id,
        user_id: user_id
    })

    const response = new Response()
    if (ishas) {
        response.FAIL = 503
        response.DATA = "您已投递过该岗位"
        ctx.body = response.getData()
    } else {
        const result = await RecordService.newRecord({
            ctx
        })
        if (result.result) {
            response.SUCCESS = 200
            response.DATA = result
        } else {
            response.FAIL = 500
            response.DATA = '错误'
        }
        ctx.body = response.getData()
    }
}

async function getAllRecord(ctx) {
    const result = await RecordService.getAllRecord({ctx})
    const response = new Response()
    response.SUCCESS = 200
    response.DATA = {
        count: result.length,
        Records: result
    }
    ctx.body = response.getData()
}

async function getRecordById(ctx) {
    const { id } = ctx.params
    const { record } = await RecordService.getRecordById({ ctx, id })
    const response = new Response()

    if(record) {
      response.SUCCESS = 200
      response.DATA = record
    }else{
      response.FAIL = 500
      response.DATA = '记录不存在'
    }
    ctx.body = response.getData()
}


async function delRecord(ctx) {
    const {
        id
    } = ctx.request.body

    const result = await RecordService.delRecord({
        id
    })

    const response = new Response()

    if (result) {
        response.SUCCESS = 200
        response.DATA = '删除成功'
    } else {
        response.FAIL = 500
        response.DATA = '删除错误'
    }
    ctx.body = response.getData()
}

async function setRecord(ctx) {
    const {
        id
    } = ctx.request.body
    const result = await RecordService.setRecord({
        ctx,
        id
    })
    const response = new Response()
    if (result) {
        response.SUCCESS = 200
        response.DATA = '修改成功'
    } else {
        response.FAIL = 500
        response.DATA = '错误'
    }
    ctx.body = response.getData()
}

module.exports = {
    newRecord,
    getAllRecord,
    delRecord,
    setRecord,
    getRecordById
}